const MessageServices = require("../services/messageServices");

class MessageControllers {
  constructor() {
    // 显式地将所有方法绑定到类的实例
    this.addNewMsg = this.addNewMsg.bind(this);
    this.delMsgById = this.delMsgById.bind(this);
    this.getAllMsgs = this.getAllMsgs.bind(this);
    this.getMsgBySendId = this.getMsgBySendId.bind(this);
    this.getMsgByAcceptId = this.getMsgByAcceptId.bind(this);
    this.setMsgAsReadById = this.setMsgAsReadById.bind(this);
    this.getSystemMsgs = this.getSystemMsgs.bind(this);
    this.updateMsgById = this.updateMsgById.bind(this)
  }

  handleRequest = async (serviceMethod, req, res, successStatus = 200) => {
    try {
      const result = await serviceMethod(req);
      if (result.code === 0) {
        res.status(successStatus).json(result); // 成功
      } else {
        res.status(result.statusCode || 400).json(result); // 失败
      }
    } catch (error) {
      res.status(500).json({
        code: 1,
        msg: "服务器内部错误: " + error.message,
      });
    }
  };

  /**
   * 新增站内信
   * @param {Object} req - 请求对象
   * @param {Object} res - 响应对象
   */
  async addNewMsg(req, res) {
    const { title, content, sendId, acceptId } = req.body;

    // 校验必要字段
    if (!title || !content || !sendId || !acceptId) {
      return res.status(400).json({
        code: 1,
        msg: "标题、内容、发送人ID和接收人ID不能为空",
      });
    }

    await this.handleRequest(
      () => MessageServices.addNewMsg(req.body),
      req,
      res,
      201
    );
  }

  /**
   * 根据 ID 删除站内信
   * @param {Object} req - 请求对象
   * @param {Object} res - 响应对象
   */
  async delMsgById(req, res) {
    await this.handleRequest(
      () => MessageServices.delMsgById(req.params.id),
      req,
      res,
      200
    );
  }

  /**
   * 获取全部站内信（带分页和过滤）
   * @param {Object} req - 请求对象
   * @param {Object} res - 响应对象
   */
  async getAllMsgs(req, res) {
    await this.handleRequest(
      () => MessageServices.getAllMsgs(req.query),
      req,
      res,
      200
    );
  }

  /**
   * 根据发送人ID查找站内信
   * @param {Object} req - 请求对象
   * @param {Object} res - 响应对象
   */
  async getMsgBySendId(req, res) {
    const sendId = req.params.sendId; // 从路径参数中获取发送人ID
    const isRead = req.query.isRead; // 从查询参数中获取 isRead

    await this.handleRequest(
      () => MessageServices.getMsgBySendId(sendId, isRead),
      req,
      res,
      200
    );
  }

  /**
   * 根据接收人ID查找站内信
   * @param {Object} req - 请求对象
   * @param {Object} res - 响应对象
   */
  async getMsgByAcceptId(req, res) {
    const acceptId = req.params.acceptId; // 从路径参数中获取接收人ID
    const isRead = req.query.isRead; // 从查询参数中获取 isRead
    console.log('acceptId, isRead: ')
    console.log(acceptId, isRead)
    await this.handleRequest(
      () => MessageServices.getMsgByAcceptId(acceptId, isRead),
      req,
      res,
      200
    );
  }

  /**
   * 根据 ID 将消息标记为已读
   * @param {Object} req - 请求对象
   * @param {Object} res - 响应对象
   */
  async setMsgAsReadById(req, res) {
    await this.handleRequest(
      () => MessageServices.setMsgAsReadById(req.params.id),
      req,
      res,
      200
    );
  }

  /**
   * 获取来自系统的站内信
   * @param {Object} req - 请求对象
   * @param {Object} res - 响应对象
   */
  async getSystemMsgs(req, res) {
    await this.handleRequest(
      () => MessageServices.getSystemMsgs(),
      req,
      res,
      200
    );
  }

  /**
   * 根据 ID 更新站内信
   * @param {Object} req - 请求对象
   * @param {Object} res - 响应对象
   */
  async updateMsgById(req, res) {
    const { id } = req.params;
    const { title, content, sendId, acceptId, isRead, isSystem } = req.body;

    // 校验必要字段
    if (!title || !content) {
      return res.status(400).json({
        code: 1,
        msg: "标题和内容不能为空",
      });
    }

    await this.handleRequest(
      () => MessageServices.updateMsgById(id, req.body),
      req,
      res,
      200
    );
  }
}

module.exports = new MessageControllers();
